<template>
  <div class="cont">
    <!-- 搜索工作栏 -->
    <el-form ref="queryFormRef" :inline="true" :model="queryParams" class="cont_top" label-width="50px"
      @submit.prevent="handleQuery">
      <el-form-item label="联系方式" prop="contact" style="margin-right: 17px" label-width="74px">
        <el-input v-model="queryParams.contact" placeholder="" @keyup.enter="handleQuery" style="width: 200px" />
      </el-form-item>
      <el-form-item label="姓名" prop="name" style="margin-right: 17px">
        <el-input v-model="queryParams.name" placeholder="" @keyup.enter="handleQuery" style="width: 200px" />
      </el-form-item>
      <el-form-item label="提交时间" prop="beginTime" label-width="74px">
        <el-date-picker v-model="queryParams.beginTime"
          :default-createTime="[new Date('1 00:00:00'), new Date('1 23:59:59')]" style="width: 200px"
          end-placeholder="结束日期" start-placeholder="开始日期" type="daterange" @change="handleQuery"
          value-format="YYYY-MM-DD HH:mm:ss" />
      </el-form-item>
      <el-form-item style="margin-right: 0">
        <el-button @click="handleQuery" class="btn btnsearch">查询</el-button>
        <el-button @click="resetQuery" class="btn btnreset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-form :inline="true" class="cont_top" label-width="50px">
      <el-form-item style="margin-right: 0">
        <el-button link type="primary" @click="alldelbtn" class="btnhover" style="
            width: 90px;
            height: 30px;
            background-color: #ff9900;
            color: rgba(255, 255, 255, 1);
          ">批量删除</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table v-loading="loading" element-loading-text="Loading..." :data="list"
      style="margin-bottom: 20px; min-height: 435px" :header-cell-style="{ background: '#F0F0F0' }"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" />
      <el-table-column align="center" show-overflow-tooltip label="姓名" prop="name" min-width="160">
        <template #default="scope">
          <div class="namecont">
            {{ scope.row.name }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="联系方式" prop="contact" min-width="160" />
      <el-table-column align="center" label="景点质量" prop="scenicSpotsQuality" min-width="120">
        <template #default="scope">
          <div class="star_style">{{ scope.row.scenicSpotsQuality }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="服务态度" prop="serviceAttitude" min-width="120">
        <template #default="scope">
          <div class="star_style">{{ scope.row.serviceAttitude }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="餐饮水平" prop="cateringLevel" min-width="120">
        <template #default="scope">
          <div class="star_style">{{ scope.row.cateringLevel }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="交通便利性" prop="transportationConvenience" min-width="120">
        <template #default="scope">
          <div class="star_style">{{ scope.row.transportationConvenience }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip label="评价内容" prop="evaluateContent" min-width="300">
        <template #default="scope">
          <div class="namecont">
            {{ scope.row.evaluateContent }}
          </div>
        </template>
      </el-table-column>
      <el-table-column :formatter="dateFormatter" align="center" label="提交时间" prop="createTime" min-width="200" />
      <el-table-column align="center" label="操作" fixed="right" width="100">
        <template #default="scope">
          <el-button link type="primary" class="btnhover" @click="detailsbtn(scope.row)"
            style="color: #3c88f0">详情</el-button>
          <el-button link type="primary" class="btnhover" @click="delbtn(scope.row)"
            style="color: #3c88f0">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination style="font-size: 14px" v-model:current-page="queryParams.pageNo" :hide-on-single-page="value"
      v-model:page-size="queryParams.pageSize" :page-sizes="pageSizes" layout="total,sizes,prev, pager, next,jumper"
      :total="total" @size-change="selectchange" @current-change="handleCurrentChange" />
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { dateFormatter } from '@/utils/formatTime'
import { formatTime } from '@/utils'
import * as getTouristApi from '@/api/smartTourism/index'
const { push } = useRouter() // 路由
const selectList = ref([]) // 表格选中数据
const loading = ref(true) // 列表加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const value = ref(true)
const pageSizes = [10, 20, 50, 100]
const TimeValue = ref([])
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  contact: '',
  name: '',
  beginTime: []
})
const queryFormRef = ref() // 搜索的表单

/** 条数变化 */
const selectchange = (e) => {
  queryParams.pageSize = e
  queryParams.pageNo = 1
  getList()
}
/** 页码变化 */
const handleCurrentChange = (e) => {
  queryParams.pageNo = e
  getList()
}

/** 查询任务列表 */
const getList = async () => {
  getTouristApi
    .getTouristPage(queryParams)
    .then((res) => {
      let data = reactive(res.list)
      list.value = data
      total.value = res.total
      if (list.value.length === 0) {
        value.value = true
      } else {
        value.value = false
      }
      loading.value = false
    })
    .catch((error) => {
      loading.value = false
    })
}

/** 详情 */
const detailsbtn = (row: any) => {
  push({
    name: 'evaluationDetails',
    query: {
      id: row.id
    }
  })
}

/** 批量删除 */
const handleSelectionChange = (e) => {
  selectList.value = e
}
const alldelbtn = () => {
  if (selectList.value.length == 0) {
    ElMessage({
      message: '至少选中一条数据！',
      type: 'warning'
    })
    return
  }

  ElMessageBox.confirm('此操作将永久删除选中评价, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      const idList = reactive([])
      selectList.value.forEach((item) => {
        idList.push(item.id)
      })
      getTouristApi.deleteTouristList(idList).then((res) => {
        if (res === true) {
          ElMessage.success('删除成功！')
          getList()
        }
      })
    })
    .catch(() => {
      ElMessage.info('已取消删除')
    })
}

/** 删除 */
const delbtn = (row: any) => {
  ElMessageBox.confirm('此操作将永久删除该评价, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      getTouristApi.deleteTouristList([row.id]).then((res) => {
        if (res === true) {
          ElMessage.success('删除成功！')
          getList()
        }
      })
    })
    .catch(() => {
      ElMessage.info('已取消删除')
    })
}

/** 查询按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style scoped>
.cont {
  background-color: rgba(255, 255, 255, 1);
  color: rgba(16, 16, 16, 1);
  font-size: 14px;
  text-align: center;
  min-height: 557px;
  position: relative;
}

.cont_top {
  text-align: left;
  margin: 4px 0;
}

::v-deep .el-table .cell {
  display: flex;
  justify-content: center;
}

::v-deep .el-input__wrapper.is-focus {
  box-shadow: 0 0 0 1px #cc352f;
}

::v-deep .el-range-editor.is-active {
  box-shadow: 0 0 0 1px #cc352f;
}

::v-deep .el-select__wrapper.is-focused {
  box-shadow: 0 0 0 1px #cc352f;
}

.btn {
  width: 80px;
  height: 30px;
  line-height: 20px;
  border-radius: 5px;
  font-size: 14px;
  text-align: center;
}

.btn:hover {
  opacity: 0.8;
}

.btnreset {
  background-color: rgba(255, 255, 255, 1);
  border: 1px solid rgba(187, 187, 187, 1);
  color: rgba(142, 142, 142, 1);
}

.btnsearch {
  background-color: #cc352f;
  color: rgba(255, 255, 255, 1);
  border: 1px solid #cc352f;
}

.btnhover:hover {
  opacity: 0.8;
}

.el-pagination {
  justify-content: center;
}

.star_style {
  font-size: 14px;
  color: #c0790f;
}

::v-deep .el-form-item__label {
  padding: 0 17px 0 0;
}

.namecont {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
